//
//  UITextView+LYAdd.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/5/26.
//

#import "UITextView+LYAdd.h"
#import <objc/runtime.h>
#import "LYUIMacros.h"

LYSYNTH_DUMMY_CLASS(UITextView_LYAdd)

@implementation UITextView (LYAdd)

- (NSUInteger)maxTextLength
{
    return [objc_getAssociatedObject(self, _cmd) integerValue];
}
- (void)setMaxTextLength:(NSUInteger)length
{
    objc_setAssociatedObject(self, @selector(maxTextLength), @(length), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewTextDidChange:) name:UITextViewTextDidChangeNotification object:self];
}
- (void)textViewTextDidChange:(NSNotification *)notify {
    NSString *toBeString = self.text;
    //获取高亮部分
    UITextRange *selectedRange = [self markedTextRange];
    UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
    
    //没有高亮选择的字，则对已输入的文字进行字数统计和限制
    //在iOS7下,position对象总是不为nil
    NSInteger maxLength = self.maxTextLength;
    if ( (!position ||!selectedRange) &&
        (maxLength > 0 && toBeString.length > maxLength))
    {
        NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
        if (rangeIndex.length == 1)
        {
            self.text = [toBeString substringToIndex:maxLength];
        }
        else
        {
            NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
            NSInteger tmpLength;
            if (rangeRange.length > maxLength)
            {
                tmpLength = rangeRange.length - rangeIndex.length;
            }
            else
            {
                tmpLength = rangeRange.length;
            }
            self.text = [toBeString substringWithRange:NSMakeRange(0, tmpLength)];
        }
    }
}

- (UITextView *)placeholderTextView
{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setPlaceholderTextView:(UITextView *)textView
{
    objc_setAssociatedObject(self, @selector(placeholderTextView), textView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)addPlaceholder:(NSString *)placeholder
{
    if (!self.placeholderTextView)
    {
        UITextView *textView = [[UITextView alloc] initWithFrame:self.bounds];
        textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        textView.font = self.font;
        textView.backgroundColor = [UIColor clearColor];
        textView.textColor = [UIColor grayColor];
        textView.userInteractionEnabled = NO;
        
        [self addSubview:textView];
        self.placeholderTextView = textView;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:self];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:self];
    }
    self.placeholderTextView.text = placeholder;
}

- (void)textViewDidBeginEditing:(NSNotification *)noti
{
    self.placeholderTextView.hidden = self.hasText;
}
- (void)textViewDidEndEditing:(UITextView *)noti
{
    self.placeholderTextView.hidden = !self.hasText;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
